//
//  YAYReviewController.m
//  YAYIDoctor
//
//  Created by hxp on 16/6/30.
//
//

#import "YAYReviewController.h"
#import "YAYIEditResumeController.h"
#import "YAYIResumeViewController.h"


@interface YAYReviewController ()

@property (nonatomic,strong)UILabel *buttonOne;
@property (nonatomic,strong)UILabel *buttonTwo;
@property (nonatomic,strong)UILabel *buttonThree;
@property (nonatomic,strong)UILabel  *labelOne;
@property (nonatomic,strong)UILabel  *labelTwo;
@property (nonatomic,strong)UILabel *titleLabel1;
@property (nonatomic,strong)UILabel *titleLabel2;
@property (nonatomic,strong)UILabel *titleLabel3;
@property (nonatomic,strong)UILabel *msgLabel;
@property (nonatomic,strong)UIButton *goDeliverBtn;
@property (nonatomic,weak) UIImageView *stateImageView;

@property (nonatomic,assign)NSInteger progress;

@end

@implementation YAYReviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"简历审核结果";
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
    _progress = 0;
    [self setupNav];
    [self createView];
    [self getDataSource];
    [self createMsgView];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    YAYIResumeViewController *resumeVc = self.navigationController.childViewControllers.lastObject;
    if ([resumeVc isKindOfClass:[YAYIResumeViewController class]]) {
        YAYIResumeViewController *resumeVc = self.navigationController.childViewControllers.lastObject;
        [resumeVc refreshPage];
    }
}


- (void)setupNav
{

    if (self.isComeAddOrEdit) {
        NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.childViewControllers];
        [vcArray removeObjectAtIndex:vcArray.count - 2];
        [self.navigationController setViewControllers:vcArray];
    }
    
    UIButton *backBtn =  (UIButton *)self.navigationItem.leftBarButtonItem.customView;
    [backBtn removeTarget:self.navigationController action:nil forControlEvents:UIControlEventTouchUpInside];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}


-(void)createView{
    
    CGFloat marginleft = 30;
    CGFloat marginTop = 40 + 64;
    CGFloat btnWidth = 16;
    CGFloat lineWidth = ([UIScreen mainScreen].bounds.size.width - marginleft*2 - btnWidth * 3)/2.0;
    self.buttonOne = [UILabel new];
    self.buttonOne.backgroundColor =[UIColor whiteColor];
    self.buttonOne.layer.borderWidth = 1;
    self.buttonOne.font = [UIFont systemFontOfSize:14];
    self.buttonOne.layer.borderColor = YAYIColor(151,151,151).CGColor;
    self.buttonOne.textColor = YAYIColor(151,151,151);
    self.buttonOne.text = @"1";
    self.buttonOne.textAlignment = NSTextAlignmentCenter;
    self.buttonOne.layer.masksToBounds = YES;
    self.buttonOne.layer.cornerRadius = 8;
    [self.view addSubview:self.buttonOne];
    [self.buttonOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(marginleft));
        make.top.equalTo(@(marginTop));
        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
    }];
    
    self.titleLabel1 = [UILabel new];
    self.titleLabel1.textAlignment = NSTextAlignmentCenter;
    self.titleLabel1.textColor = YAYIColor(232, 121, 117);
    self.titleLabel1.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.titleLabel1];
    [self.titleLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.buttonOne);
        make.top.mas_equalTo(self.buttonOne.mas_bottom).with.offset(8);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    self.labelOne = [UILabel new];
    self.labelOne.backgroundColor = YAYIColor(151,151,151);
    [self.view addSubview:self.labelOne];
    [self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.buttonOne.mas_right);
        make.centerY.mas_equalTo(self.buttonOne);
        make.size.mas_equalTo(CGSizeMake(lineWidth, 1));
    }];
    
    self.buttonTwo = [UILabel new];
    self.buttonTwo.backgroundColor =[UIColor whiteColor];
    self.buttonTwo.layer.borderWidth = 1;
     self.buttonTwo.font = [UIFont systemFontOfSize:14];
    self.buttonTwo.layer.borderColor = YAYIColor(151,151,151).CGColor;
    self.buttonTwo.textColor = YAYIColor(151,151,151);
    self.buttonTwo.text = @"2";
    self.buttonTwo.textAlignment = NSTextAlignmentCenter;
    self.buttonTwo.layer.masksToBounds = YES;
    self.buttonTwo.layer.cornerRadius = 8;
    [self.view addSubview:self.buttonTwo];
    [self.buttonTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.labelOne.mas_right);
        make.top.mas_equalTo(self.buttonOne);
        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
    }];
    self.titleLabel2 = [UILabel new];
    self.titleLabel2.textAlignment = NSTextAlignmentCenter;
    self.titleLabel2.textColor = YAYIColor(232, 121, 117);
    self.titleLabel2.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.titleLabel2];
    [self.titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.buttonTwo);
        make.top.mas_equalTo(self.buttonTwo.mas_bottom).with.offset(8);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    
    self.labelTwo = [UILabel new];
    self.labelTwo.backgroundColor = YAYIColor(151,151,151);
    [self.view addSubview:self.labelTwo];
    [self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.buttonTwo.mas_right);
        make.centerY.mas_equalTo(self.buttonTwo);
        make.size.mas_equalTo(CGSizeMake(lineWidth, 1));
    }];
   
    self.buttonThree = [UILabel new];
    self.buttonThree.backgroundColor =[UIColor whiteColor];
    self.buttonThree.layer.borderWidth = 1;
    self.buttonThree.layer.borderColor = YAYIColor(151,151,151).CGColor;
    self.buttonThree.textColor = YAYIColor(151,151,151);
    self.buttonThree.font = [UIFont systemFontOfSize:14];
    self.buttonThree.text = @"3";
    self.buttonThree.textAlignment = NSTextAlignmentCenter;
    self.buttonThree.layer.masksToBounds = YES;
    self.buttonThree.layer.cornerRadius = 8;
    [self.view addSubview:self.buttonThree];
    [self.buttonThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.labelTwo.mas_right);
        make.top.mas_equalTo(self.buttonOne);
        make.size.mas_equalTo(CGSizeMake(btnWidth, btnWidth));
    }];
    
    self.titleLabel3 = [UILabel new];
    self.titleLabel3.textAlignment = NSTextAlignmentCenter;
    self.titleLabel3.textColor = YAYIColor(232, 121, 117);
    self.titleLabel3.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.titleLabel3];
    [self.titleLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.buttonThree);
        make.top.mas_equalTo(self.buttonThree.mas_bottom).with.offset(8);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    
    UIImageView *stateImageView = [[UIImageView alloc] init];
    [self.view addSubview:stateImageView];
    self.stateImageView = stateImageView;
    [stateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel3.mas_bottom).offset(50);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(84);
    }];
    
}
-(void)createMsgView{
    
    CGFloat marginTop = ([UIScreen mainScreen].bounds.size.height)/2.0 + 40;
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 30*2;
    self.msgLabel = [UILabel new];
    self.msgLabel.textColor = YAYIColor(151,151,151);
    self.msgLabel.font = [UIFont systemFontOfSize:14];
    self.msgLabel.textAlignment = NSTextAlignmentCenter;
    self.msgLabel.numberOfLines = 0;
    [self.view addSubview:self.msgLabel];
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@30);
        make.top.mas_equalTo(@(marginTop));
        make.size.mas_equalTo(CGSizeMake(width, 80));
    }];
    
    
    self.goDeliverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.goDeliverBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.goDeliverBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.goDeliverBtn setBackgroundImage:[UIImage imageNamed:@"register_button_normal"] forState:UIControlStateNormal];
    self.goDeliverBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    self.goDeliverBtn.hidden = YES;
    [self.view addSubview:self.goDeliverBtn];
    [self.goDeliverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.msgLabel.mas_left);
        make.top.mas_equalTo(self.msgLabel.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 40));
    }];
}


#pragma mark - 获取数据
-(void)getDataSource{
    [YAYINetWorking getWithURL:resumeInfo_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            NSDictionary *map = dict[@"map"];
            if ([map[@"state"] integerValue] == 1) {
                NSDictionary *data =  dict[@"data"];
                
                if ([data[@"v"] integerValue] == 0) { // 审核中
                    self.buttonOne.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonOne.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    self.buttonOne.textColor = [UIColor whiteColor];
                    self.titleLabel1.text = @"已提交";
                    self.labelOne.backgroundColor = YAYIColor(232, 121, 117);
                    
                    
                    self.buttonTwo.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonTwo.textColor = [UIColor whiteColor];
                    self.titleLabel2.text = @"审核中";
                    self.buttonTwo.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    
                    // 改变图片
                    self.stateImageView.image = [UIImage imageNamed:@"resumeState1"];
                    
                    self.goDeliverBtn.hidden = YES;
                    [self.goDeliverBtn setTitle:@"去多点执业" forState:UIControlStateNormal];
                    self.msgLabel.text = @"您提交的认证申请正在审核中，我们会在三个工作日处理完毕，请耐心等待，审核结果会发送到系统消息中，请您注意查收。";
                }else if ([data[@"v"] integerValue] == 1){  // 通过审核
                    
                    _progress = 1;
                    if (self.refreshedOption) {
                        self.refreshedOption();
                    }
                    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                    [dict setValue:dict[@"mobile"] forKey:@"mobile"];
                    [dict setValue:dict[@"v"] forKey:@"v"];
                    [user setObject:dict forKey:@"reviewed"];
                    [user synchronize];
                    self.buttonOne.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonOne.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    self.buttonOne.textColor = [UIColor whiteColor];
                    self.titleLabel1.text = @"已提交";
                    self.labelOne.backgroundColor = YAYIColor(232, 121, 117);
                    
                    self.buttonTwo.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonTwo.textColor = [UIColor whiteColor];
                    self.titleLabel2.text = @"审核中";
                    self.buttonTwo.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    
                    self.labelTwo.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonThree.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonThree.textColor = [UIColor whiteColor];
                    self.titleLabel3.text = @"通过审核";
                    self.buttonThree.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    self.msgLabel.text = @"恭喜你！身份信息已通过认证！你可进行多点执业了～～";
                    
                    // 改变图片
                    self.stateImageView.image = [UIImage imageNamed:@"resumeState0"];
                    
                    self.goDeliverBtn.hidden = NO;
                     [self.goDeliverBtn setTitle:@"去多点执业" forState:UIControlStateNormal];
                }else if ([data[@"v"] integerValue] == 2){  // 未通过审核
                    
                    _progress = 2;
                    self.buttonOne.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonOne.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    self.buttonOne.textColor = [UIColor whiteColor];
                    self.titleLabel1.text = @"已提交";
                    self.labelOne.backgroundColor = YAYIColor(232, 121, 117);
                    
                    self.buttonTwo.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonTwo.textColor = [UIColor whiteColor];
                    self.titleLabel2.text = @"审核中";
                    self.buttonTwo.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    
                    self.labelTwo.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonThree.backgroundColor = YAYIColor(232, 121, 117);
                    self.buttonThree.textColor = [UIColor whiteColor];
                    self.titleLabel3.text = @"未通过审核";
                    self.buttonThree.layer.borderColor = YAYIColor(232, 121, 117).CGColor;
                    self.msgLabel.text = @"很遗憾！身份信息未通过认证！请您再认真完善一下你的简历，信息请务必属实！如有疑问请拨打我们的客服电话：123456，为您服务。";
                    
                    // 改变图片
                    self.stateImageView.image = [UIImage imageNamed:@"resumeState2"];
                    
                    self.goDeliverBtn.hidden = NO;
                     [self.goDeliverBtn setTitle:@"去完善简历" forState:UIControlStateNormal];
                }
            }
        }
    }];
}


#pragma mark - 点击事件
- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)buttonAction:(UIButton *)sender{
    if (_progress == 2) {
        YAYIEditResumeController *editView = [YAYIEditResumeController new];
        [self.navigationController pushViewController:editView animated:YES];
    }else if (_progress == 1){
        
    }
}
@end
